$('#cartnull').hide();
function getCart() {
	$.ajax({
		url: 'http://localhost:3000/admin/userCart',
		method: 'GET',
		async: false,
		data: { userId: sessionStorage.getItem('userid') },
		success: function (data) {
			var nums = 0;
			var summoney = 0;
			// console.log(data);
			if (data.data.length == 0) {
				$('.cart-list').hide();
				$('.cart-foot').hide();
				$('#cartnull').show();
			} else {
				$('.cart-list')[0].innerHTML = ``;
				data.data.forEach(function (cart, index) {
					nums = cart.amount * cart.price;
					summoney += nums;
					$('.cart-list')[0].innerHTML += `
    <div class="cart-cont">
    <ul class="ul1">
      <li class="li2">
        <a onclick="datai(${cart.good_id})" href="javascript:void(0)">
          <img src="http://localhost:3000/update/${cart.cover}">
        </a>
      </li>
      <li class="li3">
        <p class="goods-name">${cart.name}</p>
        <p class="goods-des">${cart.intro}</p>
      </li>
      <li class="li4">
        <p><span>${cart.spec}</span><br></p>
      </li>
      <li class="li5">￥<span class="price">${cart.price}</span></li>
      <li class="li6">
          <span class="cut" style="cursor: pointer;${cart.amount == 1 ? '' : 'display:none'}"  disabled="disabled">-</span>
    
          <a href="javascript:void(0)">
            <span class="cut" style="cursor: pointer;${cart.amount == 1 ? 'display:none' : ''}"   onclick="cartLess(this,${cart.good_id})">-</span>
          </a>
        <input type="text" class="sum" value="${cart.amount}" readonly="readonly">
        <a href="javascript:void(0)">
          <span class="add" style="cursor: pointer" onclick="cartAdd(this,${cart.good_id})">+</span>
        </a>
      </li>
      <li class="li7">￥<span class="subtotal">${nums}</span></li>
      <li class="li8">
        <i style="cursor: pointer" class="iconfont icon-shanchu" onclick="cartDelete(${cart.id})"></i>
      </li>
    </ul>
    </div>
    `;
				});
			}
			$('.cart-foot')[0].innerHTML = `

     <div class="foot-right">
       <div class="mdd">
         <p class="one">
           <span>订单总额 ：<span class="price">￥<span class="m-price">${summoney}</span></span></span>
         </p>
       </div>
       <a href="orderSave">
         <a href="javascript:void(0)" onclick="addor()"><span class="js" style="background: rgb(255, 113, 43);">去结算</span></a>
       </a>

   </div>
     `;
		}
	});
}
getCart();
// 添加
function cartAdd(obj, id) {
	// console.log($(obj).parents('.ul1').find('.price').text());
	// console.log(parseInt($(obj).parents('.ul1').find('.sum').val()));

	var num = Number($(obj)[0].parentNode.parentNode.children[2].value) + 1;
	console.log(num);
	var index = layer.load();
	// console.log($(obj)[0].parentNode.parentNode.children[2].value);
	$.post('http://localhost:3000/admin/cartGoodEditNum', { amount: num, good_id: id, user_id: sessionStorage.getItem('userid') }, function (data) {
		layer.close(index);
		console.log(data);
		// reloadTotal(); // 更新订单总额
		getCart();
		layer.msg('成功', { icon: 1, time: 500 }, function () {
			cartn();
		});
	});
}

// 减少
function cartLess(obj, id) {
	var num = Number($(obj)[0].parentNode.parentNode.children[2].value) - 1;
	var index = layer.load();
	$.post('http://localhost:3000/admin/cartGoodEditNum', { amount: num, good_id: id, user_id: sessionStorage.getItem('userid') }, function (data) {
		layer.close(index);
		// reloadTotal(); // 更新订单总额
		getCart();
		layer.msg('成功', { icon: 1, time: 500 }, function () {
			cartn();
		});
	});
}

// 删除
function cartDelete(id) {
	layer.confirm(
		'确定要删除吗？',
		{
			btn: ['确定', '取消']
		},
		function () {
			$.ajax({
				method: 'GET',
				url: 'http://localhost:3000/admin/cartGoodDel',
				data: {
					id: id
				},
				success: function () {
					layer.msg('成功', { icon: 1, time: 800 });
					getCart();
					cartn();
				}
			});
		},
		function () {}
	);
}

function addor() {
	var userobj = JSON.parse(sessionStorage.getItem('userobj'));
	$.ajax({
		url: 'http://localhost:3000/admin/orderAdd',
		method: 'POST',
		async: false,
		data: { user_id: userobj.id, paytype: 0, name: userobj.name, address: userobj.address, phone: userobj.phone },
		success: function (data) {
			sessionStorage.setItem('oid', data.data.order_id);
			getCart();
			cartn();
			layer.msg('成功！', { icon: 1, time: 300 }, function () {
				location.href = './pay.html';
			});
		}
	});
}
